﻿using System;
using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;
using UnityEngine.Playables;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class RefreshSceneDetector : LatentActionNodeBase
    {
        [RequiredField]
        public BBParameter<Vector3> position;
        public BBParameter<Vector3> size;

        private static SceneTransformDetector detector;
        private static GameObject gameObject;
        
        private bool stopped = false;

        public override bool allowRoutineQueueing { get { return false; } }
        
        public IEnumerator Invoke()
        {
            if (detector == null)
            {
                gameObject = new GameObject("[BluePrintSceneDetector]");
                detector = gameObject.AddComponent<SceneTransformDetector>();
            }

            if (size.value == Vector3.zero)
            {
                detector.detectorSize = new Vector3(100, 50, 100);
            }
            else
            {
                detector.detectorSize = size.value;
            }
            gameObject.transform.position = position.value;
            LibertySceneManager.RefreshDetectorImmediately(detector);
            yield return new UnityEngine.WaitUntil(() =>
            {
                return true;
            });
        }
        
        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            node.AddFlowInput("In", (f) => { Begin(Invoke(), f); });
        }
    }
}